package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/destination")
@Controller
public class DestinationController {
    @Autowired
    private IDestinationService destinationService;


    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") DestinationQuery qo){
        //根据请求查询分页数据
        Page<Destination> page = destinationService.query(qo);
        //加到模型中
        model.addAttribute("page", page);
        //将汉堡也加到页面中
        List<Destination> toasts = destinationService.getToasts(qo);
        model.addAttribute("toasts", toasts);
        return "destination/list";
    }

    @RequestMapping("/updateInfo")
    @ResponseBody
    public Object updateInfo(String id,String info){
        //更新描述
        destinationService.updateInfo(id,info);
        return JsonResult.success();
    }


    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(){
        //查询出所有地区数据,返回到ajax
        List<Destination> list = destinationService.selectAll();
        return list;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Destination destination){
        //调用服务层方法
        if (StringUtils.hasLength(destination.getId())){
            //调用更新方法
            destinationService.update(destination);
        }else {
            //调用保存方法
            destinationService.save(destination);
        }
        //返回执行成功
        return JsonResult.success();
    }





    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        //删除
        destinationService.deleteById(id);
        //返回jsonresult
        return JsonResult.success();
    }

}
